package com.ocom.company.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ocom.common.constant.CompanyServiceUrl;
import com.ocom.common.entity.master.EntityCompanykeyInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.company.service.CompanyKeyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CompanyKeyInfoController {

    @Autowired
    private CompanyKeyInfoService companyKeyInfoService;
    /**
     * 添加单位秘钥信息
     * */
    @PostMapping(CompanyServiceUrl.URL_AddCompanyKeyInfo)
    public Result AddCompanyKeyInfo(@Validated @RequestBody EntityCompanykeyInfo companykeyInfo){
        try {
            companyKeyInfoService.AddCompanyKeyInfo(companykeyInfo);
        }catch (Exception exception){
            exception.printStackTrace();
        }

        return ResultUtil.success();
    }

    /*
     * @Description:查询单位秘钥信息
     * @Author: lu 
     * @Date: 2022/4/26 13:22
     * @param comId: 
     * @param page: 
     * @param size:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(CompanyServiceUrl.URL_GetCompanyKeyInfo)
    public Result getCompanyKeyInfo(Long comId,Integer page,Integer size){
        if (comId==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        if (page !=null && size !=null && page !=-1 && size !=-1){
            PageInfo<EntityCompanykeyInfo> pageInfo=companyKeyInfoService.selectCompanyKeyListPage(comId,page,size);
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("total",pageInfo.getTotal());
            jsonObject.put("list",pageInfo.getList());
            return ResultUtil.success(jsonObject);
        }else {
            List<EntityCompanykeyInfo> list=companyKeyInfoService.selectCompanyKeyList(comId);
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("total",list.size());
            jsonObject.put("list",list);
            return ResultUtil.success(jsonObject);
        }
    }

    /*
     * @Description:更新单位通讯秘钥
     * @Author: lu 
     * @Date: 2022/4/26 15:26
     * @param comId: 
     * @param commKey:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(CompanyServiceUrl.URL_UpdateCompanyCommKey)
    public Result updateCompanyCommKey(Long comId,String commKey){
        if (comId==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }

        int i=companyKeyInfoService.updateCompanyCommKey(comId,commKey);
        if (i>0){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_3026);
        }
    }

    @PostMapping(CompanyServiceUrl.URL_getCompanyKeyInfoOne)
    public Result getCompanyKeyInfoOne(Long companyId){
        EntityCompanykeyInfo entityCompanykeyInfo=companyKeyInfoService.getCompanyKeyInfo(companyId);
        if(entityCompanykeyInfo==null){
            return ResultUtil.error(ResultEnum.Err_2031);
        }
        return ResultUtil.success(entityCompanykeyInfo);
    }
}
